package org.skydiveapplibrary.ui;

import org.skydiveapplibrary.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

public class FreefallTimeDialog extends AlertDialog
{
	// listener definition
	public interface OnFreefallTimeSetListener
	{
		void onFreefallTimeSet(FreefallTimeDialog dialog, int freefallTime);
	}
	
	// listeners
	private OnFreefallTimeSetListener onFreefallTimeSetListener;
	private OnClickListener okClickListener = new OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			handleOKClick();
		}
	};
	private OnClickListener cancelClickListener = new OnClickListener()
	{
		@Override
		public void onClick(DialogInterface arg0, int arg1)
		{
			dismiss();
		}
	};
	
	// data
	private int mFreefallTime = 0;
	
	public FreefallTimeDialog(
			Context context,
			int freefallTime,
			OnFreefallTimeSetListener onFreefallTimeSetListener)
	{
		super(context);
		
		this.onFreefallTimeSetListener = onFreefallTimeSetListener;
		
		// get view
		View view = View.inflate(context, R.layout.freefall_time_dialog, null);
		
		// init view
		setView(view, 10, 10, 10, 10);

		setButton(
				context.getString(android.R.string.ok),
				okClickListener);
		setButton2(
				context.getString(android.R.string.cancel),
				cancelClickListener);
		setTitle(context.getString(R.string.freefall_time_title));
		
		// init data
		mFreefallTime = freefallTime;
	}
	
	public void show()
	{
		super.show();
		// init fields
		TextView ffTimeHoursField = (TextView)findViewById(R.id.FreefallTimeHoursField);
		TextView ffTimeMinutesField = (TextView)findViewById(R.id.FreefallTimeMinutesField);
		TextView ffTimeSecondsField = (TextView)findViewById(R.id.FreefallTimeSecondsField);
		int hours = mFreefallTime / 3600;
		int secondsLeftFromHours = mFreefallTime % 3600;
		int minutes = secondsLeftFromHours / 60;
		int seconds = secondsLeftFromHours % 60;
		ffTimeHoursField.setText(FormatUtility.toNumericString(hours));
		ffTimeMinutesField.setText(FormatUtility.toNumericString(minutes));
		ffTimeSecondsField.setText(FormatUtility.toNumericString(seconds));
	}
	
	private void handleOKClick()
	{
		// get new freefall time
		int freefallTimeInSeconds = 0;
		TextView ffTimeHoursField = (TextView)findViewById(R.id.FreefallTimeHoursField);
		TextView ffTimeMinutesField = (TextView)findViewById(R.id.FreefallTimeMinutesField);
		TextView ffTimeSecondsField = (TextView)findViewById(R.id.FreefallTimeSecondsField);

		// get hours
		String hoursString = ffTimeHoursField.getText() == null ? null : ffTimeHoursField.getText().toString();
		freefallTimeInSeconds += FormatUtility.toInteger(hoursString, 0) * 60 * 60;
		// get minutes
		String minutesString = ffTimeMinutesField.getText() == null ? null : ffTimeMinutesField.getText().toString();
		freefallTimeInSeconds += FormatUtility.toInteger(minutesString, 0) * 60;
		// get seconds
		String secondsString = ffTimeSecondsField.getText() == null ? null : ffTimeSecondsField.getText().toString();
		freefallTimeInSeconds += FormatUtility.toInteger(secondsString, 0);
		
		if (onFreefallTimeSetListener != null)
		{
			onFreefallTimeSetListener.onFreefallTimeSet(this, freefallTimeInSeconds);
		}
		
		// dismiss dialog
		dismiss();
	}
}
